package org.bonorsoft.modules.control.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.modules.control.entity.*;
import org.bonorsoft.modules.control.service.*;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * @Description: 二维码扫描页面显示字段
 * @Author: WilliamYe
 * @Date: 2021-09-08
 * @Version: V1.0
 */
@Slf4j
@Controller
@RequestMapping("/control/display")
public class DisplayController {
    private String prefix = "control/display";
    @Autowired
    private IAddressService addressService;
    @Autowired
    private IHouseService houseService;
    @Autowired
    private IUnitService unitService;
    @Autowired
    private IHouseDisplayService houseDisplayService;
    @Autowired
    private IUnitDisplayService unitDisplayService;

    /**
     * 设置显示内容
     */
    @GetMapping("/setDisplay/{dzbm}")
    public String setDisplay(@PathVariable("dzbm") String dzbm, ModelMap map) {
        Address base = addressService.getById(dzbm);
        int houseCount = houseService.count(new QueryWrapper<House>().eq("dzbm", dzbm));
        int unitCount = unitService.count(new QueryWrapper<Unit>().eq("dwdz_dzbm", dzbm));
        if (houseCount > 0) {
            int houseDisplayCount = houseDisplayService.count(new QueryWrapper<HouseDisplay>().eq("dzbm", dzbm));
            House house = houseService.getOne(new QueryWrapper<House>().eq("dzbm", dzbm));
            map.put("dzmc", house.getDzmc());
            if (houseDisplayCount > 0) {
                HouseDisplay houseDisplay = houseDisplayService.getOne(new QueryWrapper<HouseDisplay>().eq("dzbm", dzbm));
                map.put("houseDisplay", houseDisplay);
            } else {
                HouseDisplay houseDisplay = new HouseDisplay();
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                houseDisplay.setHouseDisplayId(uuid);
                houseDisplay.setDzbm(dzbm);
                map.put("houseDisplay", houseDisplay);
            }
            return prefix + "/setHouseDisplay";
        } else if (unitCount > 0) {
            int unitDisplayCount = unitDisplayService.count(new QueryWrapper<UnitDisplay>().eq("dwdz_dzbm", dzbm));
            Unit unit = unitService.getOne(new QueryWrapper<Unit>().eq("dwdz_dzbm", dzbm));
            map.put("dwmc", unit.getDwmc());
            if (unitDisplayCount > 0) {
                UnitDisplay unitDisplay = unitDisplayService.getOne(new QueryWrapper<UnitDisplay>().eq("dwdz_dzbm", dzbm));
                map.put("unitDisplay", unitDisplay);
            } else {
                UnitDisplay unitDisplay = new UnitDisplay();
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                unitDisplay.setUnitDisplayId(uuid);
                unitDisplay.setDwdzDzbm(dzbm);
                map.put("unitDisplay", unitDisplay);
            }
            return prefix + "/setUnitDisplay";
        } else {
            map.put("base",base);
            return prefix + "/setBaseDisplay";
        }
    }

    @AutoLog(value = "设置房屋扫描页面显示字段")
    @RequiresPermissions("control:display:setHouseDisplay")
    @PostMapping(value = "/setHouseDisplay")
    @ResponseBody
    public Result<?> setHouseDisplay(@Validated HouseDisplay houseDisplay) {
        int houseDisplayCount = houseDisplayService.count(new QueryWrapper<HouseDisplay>().eq("dzbm", houseDisplay.getDzbm()));
        if (houseDisplayCount > 0) {
            houseDisplayService.updateById(houseDisplay);
        } else {
            houseDisplayService.save(houseDisplay);
        }
        return Result.ok("编辑成功!");
    }

    @AutoLog(value = "设置单位扫描页面显示字段")
    @RequiresPermissions("control:display:setUnitDisplay")
    @PostMapping(value = "/setUnitDisplay")
    @ResponseBody
    public Result<?> setUnitDisplay(@Validated UnitDisplay unitDisplay) {
        int unitDisplayCount = unitDisplayService.count(new QueryWrapper<UnitDisplay>().eq("dwdz_dzbm", unitDisplay.getDwdzDzbm()));
        if (unitDisplayCount > 0) {
            unitDisplayService.updateById(unitDisplay);
        } else {
            unitDisplayService.save(unitDisplay);
        }
        return Result.ok("编辑成功!");
    }


}
